﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

public class InputParamController
{
    public class InputParam
    {
        public string token;
        public long duration;
        public DateTime startat;
        public long timeoffset;
        public int mysterious;
        public string[] device;
    }

    public const string TIME_FORMAT = "yyyy/MM/dd/HH:mm:ss.fff";

    public const string KEY_TOKEN = "token";
    public const string KEY_DURATION = "time";
    public const string KEY_START_AT = "startat";
    public const string KEY_TIME_OFFSET = "timeparam";
    public const string KEY_MYSTERIOUS = "mysterious";
    public const string KEY_ID_0 = "id0";
    public const string KEY_ID_1 = "id1";
    public const string KEY_ID_2 = "id2";

    public const string NOT_FILLED_VALUE = "NotFilled";

    private readonly static string DEFAULT_VALUE_TOKEN = "91yrf3QKQStxlIX3RLVOUwVJAXRk28FFLGwh";
    private readonly static string DEFAULT_VALUE_DURATION = "80";
    private readonly static string DEFAULT_VALUE_START_AT = DateTime.Now.ToString(TIME_FORMAT);
    private readonly static string DEFAULT_VALUE_TIME_OFFSET = "20";
    private readonly static string DEFAULT_VALUE_MYSTERIOUS = "-1";
    private readonly static string[] DEFAULT_VALUE_DEVICE = new string[] { "10000", "10010" };

    public static InputParam GetInputValue()
    {
        var result = new InputParam();
        result.token = m_GetInputToken();
        result.duration = m_GetInputDuration();
        result.startat = m_GetInputStartAtTime();
        result.timeoffset = m_GetInputTimeOffset();
        result.mysterious = m_GetInputMysterious();
        result.device = m_GetInputDevices();
        return result;
    }
    private static string m_GetInputToken()
    {
        var str_pinput_token = m_GetEnvironmentValue(KEY_TOKEN);
        if (str_pinput_token == NOT_FILLED_VALUE)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [token] is not filled  !! so we use build in value :{2}", KEY_TOKEN, str_pinput_token, DEFAULT_VALUE_TOKEN));
            str_pinput_token = DEFAULT_VALUE_TOKEN;
        }
        return str_pinput_token;
    }
    private static long m_GetInputDuration()
    {
        var str_input_duration = m_GetEnvironmentValue(KEY_DURATION);
        if (str_input_duration == NOT_FILLED_VALUE)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [durationg time] is not filled !! so we use build in value :{2}", KEY_DURATION, str_input_duration, DEFAULT_VALUE_DURATION));
            str_input_duration = DEFAULT_VALUE_DURATION;
        }
        var isSuccFormat = false;
        long durationtime;
        isSuccFormat = long.TryParse(str_input_duration, out durationtime);
        if (!isSuccFormat)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [duration time] format wrong !! so we use build in value :{2}", KEY_DURATION, str_input_duration, DEFAULT_VALUE_DURATION));
            long.TryParse(DEFAULT_VALUE_DURATION, out durationtime);
        }
        return durationtime;
    }
    private static DateTime m_GetInputStartAtTime()
    {
        var str_input_startat = m_GetEnvironmentValue(KEY_START_AT);
        if (str_input_startat == NOT_FILLED_VALUE)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [start at time] is not filled !! so we use build in value :{2}", KEY_START_AT, str_input_startat, DEFAULT_VALUE_START_AT));
            str_input_startat = DEFAULT_VALUE_START_AT;
        }
        var isSuccFormat = false;
        DateTime startattime;
        isSuccFormat = DateTime.TryParse(str_input_startat, new DateTimeFormatInfo() { ShortDatePattern = TIME_FORMAT }, DateTimeStyles.None, out startattime);
        if (!isSuccFormat)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [start at time] format wrong !! so we use build in value :{2}", KEY_START_AT, str_input_startat, DEFAULT_VALUE_START_AT));
            DateTime.TryParse(DEFAULT_VALUE_START_AT, new DateTimeFormatInfo() { ShortDatePattern = TIME_FORMAT }, DateTimeStyles.None, out startattime);
        }
        return startattime;
    }
    private static long m_GetInputTimeOffset()
    {
        var str_input_timeoffset = m_GetEnvironmentValue(KEY_TIME_OFFSET);
        if (str_input_timeoffset == NOT_FILLED_VALUE)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [time offset] is not filled !! so we use build in value :{2}", KEY_TIME_OFFSET, str_input_timeoffset, DEFAULT_VALUE_TIME_OFFSET));
            str_input_timeoffset = DEFAULT_VALUE_TIME_OFFSET;
        }
        var isSuccFormat = false;
        long timeoffset;
        isSuccFormat = long.TryParse(str_input_timeoffset, out timeoffset);
        if (!isSuccFormat)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [time offset] format wrong !! so we use build in value :{2}", KEY_TIME_OFFSET, str_input_timeoffset, DEFAULT_VALUE_TIME_OFFSET));
            long.TryParse(DEFAULT_VALUE_TIME_OFFSET, out timeoffset);
        }
        return timeoffset;
    }
    private static int m_GetInputMysterious()
    {
        var str_pinput_mysterious = m_GetEnvironmentValue(KEY_MYSTERIOUS);
        if (str_pinput_mysterious == NOT_FILLED_VALUE)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [mysterious] is not filled  !! so we use build in value :{2}", KEY_MYSTERIOUS, str_pinput_mysterious, DEFAULT_VALUE_MYSTERIOUS));
            str_pinput_mysterious = DEFAULT_VALUE_MYSTERIOUS;
        }
        var isSuccFormat = false;
        int mysterious;
        isSuccFormat = int.TryParse(str_pinput_mysterious, out mysterious);
        if (!isSuccFormat)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} --> [mysterious] format wrong !! so we use build in value :{2}", KEY_MYSTERIOUS, str_pinput_mysterious, DEFAULT_VALUE_MYSTERIOUS));
            int.TryParse(DEFAULT_VALUE_MYSTERIOUS, out mysterious);
        }
        return mysterious;
    }
    private static string[] m_GetInputDevices()
    {
        var inputDevices = new List<string>();
        var id0 = m_GetEnvironmentValue(KEY_ID_0);
        var id1 = m_GetEnvironmentValue(KEY_ID_1);
        var id2 = m_GetEnvironmentValue(KEY_ID_2);
        inputDevices.Add(id0);
        inputDevices.Add(id1);
        inputDevices.Add(id2);
        for (int index = 0; index < inputDevices.Count; ++index)
        {
            if (inputDevices[index] == NOT_FILLED_VALUE)
            {
                inputDevices.RemoveAt(index);
                index--;
            }
        }
        if (inputDevices.Count == 0)
        {
            Debug.LogWarning(string.Format("[{0}]:{1} ,[{2}]:{3} ,[{4}]:{5}, [device] is not filled  !! so we use build in value :{6}", KEY_ID_0, id0, KEY_ID_1, id1, KEY_ID_2, id2, "Null"));
            return DEFAULT_VALUE_DEVICE;
        }
        else return inputDevices.ToArray();
    }


    private static string m_GetEnvironmentValue(string param)
    {
        string target = NOT_FILLED_VALUE;
        string[] args = Environment.GetCommandLineArgs();
        foreach (string arg in args)
        {
            if (arg.Contains("="))
            {
                string[] str = arg.Split('=');
                if (str.Length == 2 && str[0] != null && str[1] != null)
                {
                    if (str[0].Equals(param))
                    {
                        target = str[1];
                        break;
                    }
                }
            }
        }
        return target;
    }
}
